﻿using UnityEngine;
using System.Collections;
using NhomTre_XocXoc;
public class Button_DatCua : MonoBehaviour {
	public static CuaDat[] cuadat;
	public int cua;
	private Vector3 moveToward;
	private Animator animator;
	private GameObject xu;
	private string loaixu;
	private long soTienDat=0;
	public static long TienXu;
	public GameObject Player;
	private bool chodat;
	private Object[] cacxu;
	private int dem=0;
	public GUIText textTienCua;
	public GUIText textTienDat;
	private long tienngchoi;
	// Use this for initialization
	void Start () {

		textTienCua.text = "";
		textTienDat.text = "";
		animator = gameObject.GetComponent<Animator> ();
		moveToward.z = 0f;
		cuadat = new CuaDat[2];
		cuadat [0] = new CuaDat(0,0);
		cuadat [1] = new CuaDat(0,0);
		cacxu = new Object[100];

		}
	
	// Update is called once per frame
	void Update () {
		tienngchoi = PlayerController.soTien;
		chodat = XocDiaController.chophepdat;
		loaixu = XuController.tenxu;
		xu = GameObject.FindWithTag (loaixu);
		if (loaixu=="xu_10k") {
			TienXu= 10000;
		}
		if (loaixu=="xu_20k") {
			TienXu= 20000;
		}
		if (loaixu=="xu_50k") {
			TienXu= 50000;
		}
		if (loaixu=="xu_100k") {
			TienXu= 100000;
		}
		if (loaixu=="xu_250k") {
			TienXu= 250000;
		}
		if (XocDiaController.bdvanmoi) {
			HuyXu();
			textTienCua.text="";
			soTienDat=0;
			cuadat[0]= new CuaDat(0,0);
			cuadat[1]= new CuaDat(1,0);
		}
	}
	void OnMouseEnter()
	{
		animator.SetBool ("isChonCua", true);
		animator.SetBool ("isBoChonCua", false);
		if (loaixu == "xu_10k") {
			moveToward.x= 2.06f;
			moveToward.y=0.37f;
		}
		if (loaixu == "xu_20k") {
			moveToward.x= 2.45f;
			moveToward.y=-0.083f;
		}
		if (loaixu == "xu_50k") {
			moveToward.x= 2.18f;
			moveToward.y=0.9f;
		}
		if (loaixu == "xu_100k") {
			moveToward.x= 2.9f;
			moveToward.y=0.2f;
		}
		if (loaixu == "xu_250k") {
			moveToward.x= 2.72f;
			moveToward.y=0.96f;
		}

		if (cua == 0) {
			moveToward.x*=-1;
		}

	}
	void OnMouseExit()
	{
		animator.SetBool ("isChonCua", false);
		animator.SetBool ("isBoChonCua", true);
	}
	void OnMouseDown()
	{		if (chodat==true) {

			moveToward.y += 0.015f;
			if (tienngchoi>0 && tienngchoi>=TienXu) {
				PlayerController.soTien-=TienXu;
				Debug.Log("AAAAAAAAAAAA");
				soTienDat+=TienXu;
				if (cua==1) {
					cuadat[1]= new CuaDat(1,soTienDat);
					
				}
				if (cua == 0) {
					cuadat[0]= new CuaDat(0,soTienDat);
				}

				textTienDat.text="-" +TienXu.ToString()+"$";
				textTienCua.text=soTienDat.ToString()+"$";
				cacxu[dem]=	Instantiate (xu, moveToward, transform.rotation);
				dem++;
			}
			if (tienngchoi<=0) {
				textTienDat.text="Ban khong du tien dat nua!";
			}


		}



	}
	void OnMouseUp()
	{ 
		textTienDat.text="";
	}

	void HuyXu()
	{
		for (int i = 0; i < dem; i++) {
			Destroy(cacxu[i]);
		}
	}

}
